package com.sttk.fastMark.camera.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.basic.dialog.BaseSmartDialog;
import com.sttk.fastMark.camera.databinding.DialogFblBinding;
import com.sttk.fastMark.camera.databinding.DialogFormatBinding;

public class FblDialog extends BaseSmartDialog<DialogFblBinding> implements View.OnClickListener {

    private String fbl = "1080X960";
    private boolean isCustom;

    public FblDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fbl;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        // 设置输入框聚焦监听
        mDataBinding.etWidth.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    clearSelection();
                    isCustom = true;
                }
            }
        });

        mDataBinding.etHeight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    clearSelection();
                    isCustom = true;
                }
            }
        });

        mDataBinding.tvFbl1.setOnClickListener(this);
        mDataBinding.tvFbl2.setOnClickListener(this);
        mDataBinding.tvFbl3.setOnClickListener(this);
        mDataBinding.tvFbl4.setOnClickListener(this);
        mDataBinding.tvFbl5.setOnClickListener(this);
        mDataBinding.tvFbl6.setOnClickListener(this);
        mDataBinding.tvFbl7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            if (isCustom) {
                String width = mDataBinding.etWidth.getText().toString().trim();
                String height = mDataBinding.etHeight.getText().toString().trim();
                if (TextUtils.isEmpty(width)) {
                    ToastUtils.showShort(getContext().getString(R.string.please_input_width));
                    return;
                }
                if (TextUtils.isEmpty(height)) {
                    ToastUtils.showShort(getContext().getString(R.string.please_input_height));
                    return;
                }
                int w = Integer.parseInt(width);
                int h = Integer.parseInt(height);
                if (w < 480 || w > 1920) {
                    ToastUtils.showShort(getContext().getString(R.string.width_tip));
                    return;
                }
                if (h < 480 || h > 1920) {
                    ToastUtils.showShort(getContext().getString(R.string.width_tip));
                    return;
                }
                fbl = width + "X" + height;
            }
            dismiss();
            if (listener != null) {
                listener.clickConfirm(fbl);
            }
        } else if (id == R.id.tvFbl1) {
            clearSelection();
            mDataBinding.tvFbl1.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl1.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text1);
        } else if (id == R.id.tvFbl2) {
            clearSelection();
            mDataBinding.tvFbl2.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl2.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text2);
        } else if (id == R.id.tvFbl3) {
            clearSelection();
            mDataBinding.tvFbl3.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl3.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text3);
        } else if (id == R.id.tvFbl4) {
            clearSelection();
            mDataBinding.tvFbl4.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl4.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text4);
        } else if (id == R.id.tvFbl5) {
            clearSelection();
            mDataBinding.tvFbl5.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl5.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text5);
        } else if (id == R.id.tvFbl6) {
            clearSelection();
            mDataBinding.tvFbl6.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl6.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text6);
        } else if (id == R.id.tvFbl7) {
            clearSelection();
            mDataBinding.tvFbl7.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvFbl7.setBackgroundResource(R.drawable.format_sel);
            fbl = getContext().getString(R.string.fbl_text7);
        }
    }

    private void clearSelection() {
        isCustom = false;

        mDataBinding.tvFbl1.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl1.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl2.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl2.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl3.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl3.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl4.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl4.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl5.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl5.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl6.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl6.setBackgroundResource(R.drawable.format_bg);
        mDataBinding.tvFbl7.setTextColor(Color.parseColor("#000000"));
        mDataBinding.tvFbl7.setBackgroundResource(R.drawable.format_bg);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String fbl);
    }
}
